#include "../catch.hpp"

#include "common/comm/Epoll.h"
#include "common/log/log.h"
#include "common/sys/ThreadPool.h"
#include "common/comm/AgentManager.h"
#include "common/comm/SocketAddress.h"
#include "common/comm/TCPListenAgent.h"
#include "common/comm/TaskManager.h"

#include "watchdog/WatchdogClientAgent.h"
#include "watchdog/WatchdogServerAgent.h"
#include "watchdog/TimerTestAgent.h"

// 一次wait能读取的最大epollEvent
int const EPOLLSIZE = 64;

// 服务端
ThreadPool *g_pThreadPool = NULL;
ThreadPoolDispatcher *g_pDispatcher = NULL;
SocketAddress servaddr;
TCPListenAgent<WatchdogClientAgent> *g_pListenCA = NULL;
// 客户端
WatchdogServerAgent *g_pServerAgent = NULL;

TEST_CASE("WatchdogClientAgent"){
    SECTION("readBack && writeBack && sendPackage && recycler"){
        REQUIRE(Epoll::getInstance()->initialize(EPOLLSIZE) == SUCCESSFUL);
        // 服务端
        servaddr.setPort(9999);
        g_pListenCA = (AgentManager::getInstance())->createAgent<TCPListenAgent<WatchdogClientAgent> >(servaddr);
        g_pListenCA->init();

        // 客户端
        servaddr.setAddress("127.0.0.1", 9999);
        g_pServerAgent = (AgentManager::getInstance())->createAgent<WatchdogServerAgent>(servaddr);
        g_pServerAgent->init();

        // 设置定时器退出epoll
        TimerTestAgent *pTimer = (AgentManager::getInstance())->createAgent<TimerTestAgent>();
        struct timespec starttime, intervaltime;
        starttime.tv_sec = 1;
        starttime.tv_nsec = 0;
        intervaltime.tv_sec = 0;
        intervaltime.tv_nsec = 0;
        pTimer->init(starttime, intervaltime);

        // 启动epoll
        Epoll::getInstance()->run();

        // -------------资源回收-------------
        AgentManager::destroyInstance();
        TaskManager::destroyInstance();
        Epoll::destroyInstance();
        LoggerFactory::getInstance()->clear();
        LoggerFactory::destroyInstance();
    }
}